package com.lg.online_shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lg.online_shop.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserMapper extends BaseMapper<User> {

    // 根据邮箱查询用户
    @Select("SELECT * FROM user WHERE email = #{email}")
    User selectByEmail(String email);

    // 自定义插入用户信息的 SQL
    @Insert("INSERT INTO user (email, password) VALUES (#{email}, #{password})")
    int insertNewUser(String email, String password);

//    用户收藏商品
    @Insert("INSERT INTO favorite (good_id, user_id) VALUES (#{goodId}, #{userId})")
    int collectGood(Long goodId, Long userId);

//     判断用户是否收藏过商品
    @Select("SELECT COUNT(*) FROM favorite WHERE good_id = #{goodId} AND user_id = #{userId}")
    int existCollect(Long goodId, Long userId);

//     取消收藏商品
    @Insert("DELETE FROM favorite WHERE good_id = #{goodId} AND user_id = #{userId}")
    int cancelCollect(Long goodId, Long userId);

//    修改用户信息
    @Update("UPDATE user SET nickname = #{nickName}, phone = #{phone} WHERE id = #{userId}")
    int updateUser(String nickName, String phone, Long userId);

//    根据邮箱修改密码
    @Update("UPDATE user SET password = #{md5Password} WHERE email = #{email}")
    int updatePasswordByEmail(String email, String md5Password);
}
